Adding dukglue_register_function_property#21
Open
CallumCarmicheal wants to merge 1 commit intoAloshi:masterfrom
Open
Adding dukglue_register_function_property#21CallumCarmicheal wants to merge 1 commit intoAloshi:masterfrom
CallumCarmicheal wants to merge 1 commit intoAloshi:masterfrom
Conversation
A helper function to allow the use of dukglue on Objects instead of the Global namespace.
Example code usage:
```cpp
int math_add(int x, int y) {
printf("Example function called\n");
return x + y;
}
void CreateTheObject(duk_context *ctx) {
// 1. Create a new object
// 2. Register the function as a property on the object
// 3. Store the object as global
duk_push_object(ctx);
dukglue_register_function_property(ctx, &math_add, "add");
duk_put_global_string(ctx, "Math");
}
void CreateTheObjectWithIndex(duk_context *ctx) {
// 1. Create a new object
// 2. Register the function as a property on the object
// 3. Store the object as global
duk_idx_t mathObjectIdx = duk_push_object(ctx);
// lets say for what ever reason there are more values on the stack
for(int x = 0; x < 3; x++)
duk_push_null(ctx);
// We will now register the function to the original stack object's index (mathObjectIdx)
dukglue_register_function_property(ctx, &math_add, mathObjectIdx, "add");
duk_put_global_string(ctx, "Math");
// Clean up the nulls (only in the example)
for(int x = 0; x < 3; x++)
duk_pop(ctx);
}
int PlayerOnHit(int currentHealth) {
return currentHealth--;
}
void AppendToObject(duk_context *ctx) {
// 1. Find the global object "Player"
// 2. Register the function as a property
// 3. Remove "Game" from the stack
duk_get_global_string(ctx, "Game");
dukglue_register_function_property(ctx, &PlayerOnHit, "OnHit");
duk_pop(ctx);
}
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A helper function to allow the use of dukglue on Objects instead of the Global namespace.
Example code usage: